package com.module.app.pop

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.app.adapter.holder.ListPopHolder
import com.module.base.base.adapter.BaseNewAdapter
import com.module.app.bean.ListData
import com.module.base.utils.RecycleViewUtil
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.module.app.R
import com.module.base.base.adapter.BaseAdapter

class ListPop(context: Context, list: MutableList<ListData>, listener: Listener) :
    BottomPopupView(context) {

    companion object {
        fun show(context: Context?, list: MutableList<ListData>, listener: Listener) {
            if (context == null) {
                return
            }
            val payPop = XPopup.Builder(context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(ListPop(context, list, listener)) as ListPop
            payPop.show()
        }
    }

    private var recyclerview: RecyclerView? = null
    private var list: MutableList<ListData> = list
    private var listener: Listener = listener

    interface Listener {
        fun onClick(listData: ListData)
    }

    private val adapter: BaseAdapter = object : BaseAdapter() {
        override fun onCreateDefaultViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): ListPopHolder {
            return ListPopHolder(parent)
        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_list
    }

    override fun onCreate() {
        super.onCreate()
        recyclerview = findViewById(R.id.recyclerview)
        recyclerview?.layoutManager = RecycleViewUtil.getLinear(context)
        adapter.bindToRecyclerView(recyclerview)
        adapter.setNewData(list)
        adapter.setOnItemClickListener(BaseNewAdapter.OnItemClickListener<ListPopHolder, ListData> { _, bean ->
            dismiss()
            listener.onClick(bean)
        })
    }
}